<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.events.EventObserver</title>
  <script src="../../base.js"></script>
<script>
  goog.require('goog.array');
  goog.require('goog.events');
  goog.require('goog.events.EventTarget');
  goog.require('goog.testing.events.EventObserver');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

  // Return an event's type
  function getEventType(e) {
    return e.type;
  }

  function testGetEvents() {
    var observer = new goog.testing.events.EventObserver();
    var target = new goog.events.EventTarget();
    goog.events.listen(target, ['foo', 'bar', 'baz'], observer);

    var eventTypes = [
        'bar', 'baz', 'foo', 'qux', 'quux', 'corge', 'foo', 'baz'];
    goog.array.forEach(eventTypes, goog.bind(target.dispatchEvent, target));

    var replayEvents = observer.getEvents();

    assertArrayEquals('Only the listened-for event types should be remembered',
        ['bar', 'baz', 'foo', 'foo', 'baz'],
        goog.array.map(observer.getEvents(), getEventType));

    assertArrayEquals(['bar'],
        goog.array.map(observer.getEvents('bar'), getEventType));
    assertArrayEquals(['baz', 'baz'],
        goog.array.map(observer.getEvents('baz'), getEventType));
    assertArrayEquals(['foo', 'foo'],
        goog.array.map(observer.getEvents('foo'), getEventType));
  }

  function testHandleEvent() {
    var events = [
      new goog.events.Event('foo'),
      new goog.events.Event('bar'),
      new goog.events.Event('baz')
    ];

    var observer = new goog.testing.events.EventObserver();
    goog.array.forEach(events, goog.bind(observer.handleEvent, observer));

    assertArrayEquals(events, observer.getEvents());
    assertArrayEquals([events[0]], observer.getEvents('foo'));
    assertArrayEquals([events[1]], observer.getEvents('bar'));
    assertArrayEquals([events[2]], observer.getEvents('baz'));
  }

</script>
</body>
</html>
